package com.example.rss_reder;

import java.io.UnsupportedEncodingException;
import java.net.ContentHandler;
import java.net.URLEncoder;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class WebLink extends Activity {
	

	WebView wv;
	ProgressBar pb;
	String url = new String();
	List list;
	DBAdapter db;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "스크랩하기");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		boolean a = false;
		Toast t;
		
		switch(item.getItemId()){
			case Menu.FIRST+1:
				db.open();
				
				db.inser(list.getName(), list.getDate(), list.getImg(1), list.getName());
				
				db.close();
				
				t = Toast.makeText(WebLink.this, "스크랩이 완료되었습니다.", Toast.LENGTH_LONG);
				t.show();
				a=true;
				break;
		}
		
		return (a || super.onOptionsItemSelected(item));
	}
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.test);
		db = new DBAdapter(WebLink.this);
	    
	    Intent i = getIntent();
	    Bundle extra = i.getExtras();
	    if(extra!=null){
	    	url = extra.getString("link");
	    	list = new List(extra.getString("name"), extra.getString("link"), extra.getString("img"), extra.getString("date"), 1);
	    }
	    
	    wv = (WebView)findViewById(R.id.wv);
	    pb = (ProgressBar)findViewById(R.id.pb);
	    pb.setVisibility(View.VISIBLE);
//	    wv.getSettings().setLoadWithOverviewMode(true);
//	    wv.getSettings().setUseWideViewPort(true);
//	    wv.setInitialScale(1);
	    wv.setWebChromeClient(new WebChromeClient(){

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				super.onProgressChanged(view, newProgress);
				if(newProgress == pb.getMax())
	            {
	               WebLink.this.setTitle(R.string.app_name);
	               WebLink.this.setProgress(0);
	               pb.setVisibility(View.GONE);
	            }
	            else
	            {
	             pb.setVisibility(View.VISIBLE);
	             WebLink.this.setTitle("Loading...");
	             WebLink.this.setProgress(newProgress*100);
	       pb.setProgress(newProgress);
	            }
			}
	    });
	   new Thread(
	    	new Runnable() {
				public void run() {
					wv.setWebViewClient(new WebViewClient(){
						@Override
						public void onPageStarted(WebView view, String url, Bitmap favicon) {
							// TODO Auto-generated method stub
							super.onPageStarted(view, url, favicon);
						}
						
						@Override
						public void onPageFinished(WebView view, String url) {
							// TODO Auto-generated method stub
							super.onPageFinished(view, url);
							pb.setProgress(100);
							pb.setVisibility(View.GONE);
						}
					});
					WebSettings set = wv.getSettings();
					set.setBuiltInZoomControls(true);
					
					String query = null;
					try {
						query = "http://www.google.co.kr/gwt/x?u="+
								URLEncoder.encode(url, "utf-8")+
						"&btnGo=%EC%A0%9C%EC%B6%9C&source=wax&ie=UTF-8&oe=UTF-8";
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					wv.loadUrl(query);
					
				}
			}
	    ).start();
	   
	    
	    
//	    String data = EncodingUtils.getString(url.getBytes(), 0, url.getBytes().length, "UTF-8");
//	    wv.loadDataWithBaseURL("", data.replaceAll("\\+", " "), "text/html", "UTF-8", "");
	    
	}

}
